package hashmap.demo1;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class StuTest {
    public static void main(String[] args) {
        //创建学生对象集合并添加数据
        HashMap<Student, String> map = new HashMap<>();
        map.put(new Student("jack", 18), "四川省");
        map.put(new Student("Tom", 19), "四川省");
        map.put(new Student("mary", 17), "四川省");
        //这里会把地址进行覆盖 因为重写了hashcode和equals方法
        Student stu = new Student("jack", 18);
        map.put(stu, "成都市");
        Set<Student> keys = map.keySet();
        for (Student key : keys) {
            System.out.println(key.getName() + ": " + key.getAge() + "->" + map.get(key));
        }
        System.out.println();
        //通过键值对进行输出
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> student : entries) {
            System.out.println(student + " :" + student.getValue());
        }
        System.out.println();
        //使用lambda进行遍历
        map.forEach((student, s) -> System.out.println(student + " :" + s));

    }
}
